package utils;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;

/**
 * TODO
 *
 * @author chenwb25485
 * @version V2.1
 * @since 2020/10/27 16:50
 */
public class image {
    public static void main(String[] args) throws IOException {
        String uploadPathStr = "D:\\Users\\DELL\\Pictures\\IMG_20201027_165857.jpg";// 上传的路径
        String uploadFile = "D:\\Users\\DELL\\Pictures\\压缩后手机.jpg";// 压缩后的路径
        try {
            Thumbnails.of(uploadPathStr)
                .scale(0.5f)
                .outputQuality(1.0f)
                .toFile(uploadFile);
            // if (!uploadPathStr.equals(uploadFile)) {
            //     (new File(uploadPathStr)).delete();
            // }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        OutputStream os = new FileOutputStream(uploadPathStr);

        Image image = ImageIO.read(new File(uploadPathStr));

        BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(image.getScaledInstance(100, 100, image.SCALE_SMOOTH), 0, 0, null);

        ImageIO.write(bufferedImage, "jpg",os);
        os.close();
    }

}
